/*
build_verbs.dm
All of the verbs used by the building system. Note, they're used by the INTERFACE, not the player themselves.
This means there is no clutter in a statpanel or whatever. However, none of the verbs are hidden, so..

Included:
	cancel				cancel the current selection of builditem
	show_build_objects	show the window of build objects
	Change_Build_Mode	change the current building mode
	Open_Portal			open the interface window to make a portal
	make_port			actually make the portal based on the info in the open_portal menu
	Get_Loc				get the current loc in x,y,z format
*/

#define ITEMS_ON_SCREEN 12

obj/hud/building
	up
		icon_state="up"
		screen_loc="15,15"

		LeftClick(mob/player/caller, location, control, params)

			if(icon_state=="up_disabled") return

			caller.cur_min_list-=ITEMS_ON_SCREEN
			caller.calc_arrows()

	down
		icon_state="down"
		screen_loc="15,2"

		LeftClick(mob/player/caller, location, control, params)

			if(icon_state=="down_disabled") return

			caller.cur_min_list+=ITEMS_ON_SCREEN
			caller.calc_arrows()

	cancel
		icon_state="cancel"
		screen_loc="15,1"

		LeftClick(mob/player/caller, location, control, params)
			caller.cancel()

	swap
		icon_state="swap_clipboard"
		screen_loc="14,1"

		LeftClick(mob/player/caller, location, control, params)
			if(icon_state=="swap_clipboard")
				caller.cur_min_list=1
				icon_state="swap_buildobjs"
				caller.cur_list = caller.clipboard
			else
				caller.cur_min_list=1
				icon_state="swap_clipboard"
				caller.cur_list = buildobjs

			caller.update_screen_items()

	buttons
		LeftClick(mob/player/caller, location, control, params)
			caller.Change_Build_Mode(icon_state)

		normal 	{ screen_loc = "1,1"; icon_state="Normal" }
		build 	{ screen_loc = "2,1"; icon_state="Building" }
		density { screen_loc = "3,1"; icon_state="Density" }
		opacity { screen_loc = "4,1"; icon_state="Opacity" }
		layer  	{ screen_loc = "5,1"; icon_state="Layer" }
		lumino 	{ screen_loc = "6,1"; icon_state="Luminosity" }
		ilumino { screen_loc = "7,1"; icon_state="Infra Luminosity" }
		copy 	{ screen_loc = "8,1"; icon_state="Copy" }
		delete 	{ screen_loc = "9,1"; icon_state="Deletion" }
		deltiles{ screen_loc = "10,1";icon_state="Delete Tiles" }

	custom
		screen_loc = "11,1"
		icon_state="customtile"

		LeftClick(mob/player/caller)
			var/icon/i = input("Choose an icon file.") as null|icon
			if(!i) return
			var/_icon_state = input(caller,"What icon_state?") in icon_states(i)
			var/_iconFile="icons_custom/[caller.ckey]/[i]"
			fcopy(i,_iconFile)
			var/_name=""
			while(!_name)
				_name = input(caller, "Name the new item") as text

			var/obj/builditems/custom/c = new(iconfile=_iconFile)
			c.icon_state=_icon_state
			c.icon=i
			c.iconFile=_iconFile
			c.name=_name
			caller.clipboard.Add(c)

	portal
		screen_loc = "12,1"
		icon='BuildTwo.dmi'
		icon_state="glass3"
		LeftClick(mob/player/caller)
			caller.Open_Portal()

	compass
		screen_loc = "13,1"
		icon_state="compass"
		LeftClick(mob/player/caller)
			caller.Get_Loc()

	//11 vertical, not counting arrow slots
mob/player
	var/tmp
		cur_min_list=1

		list/cur_list

		obj/hud/building
			up
			down

		cur_name
		cur_state
		cur_file

mob/player/proc
	calc_arrows()
		if(cur_min_list <= 1)
			cur_min_list = 1
			up.icon_state="up_disabled"

		else
			up.icon_state="up"

		if(cur_min_list > cur_list.len)
			cur_min_list = cur_list.len
			down.icon_state="down_disabled"

		else
			down.icon_state="down"

		update_screen_items()

	update_screen_items()
		clear_hud()
		var/current=3
		for(var/o=cur_min_list to cur_min_list+ITEMS_ON_SCREEN-1)
			if(cur_list.len >= o)
				var/obj/builditems/x = cur_list[o]
				if(x)
					var/obj/builditems/newobj = new x.type
					newobj.screen_loc="15,[current++]"
					newobj.layer = MOB_LAYER+5
					client.screen += newobj
					newobj.name = x.name
					newobj.icon = x.icon
					newobj.icon_state = x.icon_state
					newobj.iconFile = x.iconFile


mob/player/verb
	cancel()
		var/mob/x=usr
		x.client.mouse_pointer_icon=null
		x.current_item=null

	show_build_objects()

		cur_list = buildobjs

		up = new /obj/hud/building/up()
		down = new /obj/hud/building/down()

		client.screen += up
		client.screen += down
		client.screen += new /obj/hud/building/swap()
		client.screen += new /obj/hud/building/cancel()
		client.screen += new /obj/hud/building/custom()
		client.screen += new /obj/hud/building/portal()
		client.screen += new /obj/hud/building/compass()

		for(var/o in typesof(/obj/hud/building/buttons))
			client.screen += new o

		calc_arrows()

	Change_Build_Mode(s as text)
		cancel()
		var/mob/x=src
		switch(s)
			if("Building")			{x.buildmode="Building";src << "<font color=purple>Now creating pieces."}
			if("Layer")				{x.buildmode="Layer";src << "<font color=purple>Now modifying the layer of the pieces."}
			if("Opacity")			{x.buildmode="Opacity";src << "<font color=purple>Now modifying the opacity of the pieces."}
			if("Luminosity")		{x.buildmode="Luminosity";src << "<font color=purple>Now modifying the luminosity of the pieces"}
			if("Infra Luminosity")	{x.buildmode="Infra Luminosity";src << "<font color=purple>Now modifying the infra-luminosity of the pieces."}
			if("Density")			{x.buildmode="Density";src << "<font color=purple>Now modifying the density of the pieces."}
			if("Deletion")			{x.buildmode="Deletion";src << "<font color=purple>Now deleting pieces."}
			if("Delete Tiles")		{x.buildmode="Delete Tiles";src << "<font color=purple>Now deleting tiles."}
			if("Copy")				{x.buildmode="Copy";src << "<font color=purple>Tiles will be sent to clipboard upon clicking; click them in the clipboard to place a copied version."}
			if("Normal")			{x.buildmode="Normal";src << "<font color=purple>Nothing will happen upon clicking."}
		x.client.mouse_pointer_icon=null
		x.current_item=null

	Open_Portal()
		winset(src,"portal.maxx","text=\"Max: [world.maxx]\"")
		winset(src,"portal.maxy","text=\"Max: [world.maxy]\"")
		winset(src,"portal.maxz","text=\"Max: [world.maxz]\"")

		winset(src,"portal.curx","text=\"Cur: [x]\"")
		winset(src,"portal.cury","text=\"Cur: [y]\"")
		winset(src,"portal.curz","text=\"Cur: [z]\"")
		winshow(src,"portal", winget(src,"portal","is-visible")=="true" ? 0 : 1)

	makeport()
		var/X = round(abs(text2num(winget(src,"portal.x","text"))))
		if(!X || X>world.maxx)
			winset(src,"portal.error","text=\"X is either null or greater than maxx\"")
			return

		var/Y = round(abs(text2num(winget(src,"portal.y","text"))))
		if(!Y || Y>world.maxy)
			winset(src,"portal.error","text=\"Y is either null or greater than maxy\"")
			return

		var/Z = round(abs(text2num(winget(src,"portal.z","text"))))
		if(!Z || Z>world.maxz)
			winset(src,"portal.error","text=\"Z is either null or greater than maxz\"")
			return

		var/turf/t = locate(X,Y,Z)
		if(isunbuildable(t))
			winset(src,"portal.error","text=\"Cannot make portal there; the turf is unbuildable\"")
			return
		if(t.contents.len)
			winset(src,"portal.error","text=\"Cannot make portal there; the turf has objects on it\"")
			return

		var
			DBI = "dbi:mysql:[sql_db]:[sql_ip]:[sql_port]"
			DBConnection/my_connection = new()
			connected = my_connection.Connect(DBI,sql_user,sql_pass)

		if(!connected)
			x << "Your item [src] cannot be saved at this time."
			return

		var/obj/builditems/portal/new_item=new /obj/builditems/portal(loc,owner=ckey)
		new_item.invisibility = winget(src,"portal.invis","is-checked")=="true" ? 1 : 0
		new_item.Tele_X = X
		new_item.Tele_Y = Y
		new_item.Tele_Z = Z

		Query("INSERT INTO `[sql_objecttable]` (x,y,z,typepath,builtowner,uid,density,luminosity,invisibility,opacity,mesage,Tele_X,Tele_Y,Tele_Z,layer,infra_luminosity,icon_state,name) VALUES \
																					  ([new_item.x],[new_item.y],[new_item.z],'[new_item.type]','[new_item.builtowner]',[new_item.uid],[new_item.density],[new_item.luminosity],[new_item.invisibility],[new_item.opacity],'[new_item.mesage]',[new_item.Tele_X],[new_item.Tele_Y],[new_item.Tele_Z],[new_item.layer],[new_item.infra_luminosity],'[new_item.icon_state]','Portal');")

		new_item=new /obj/builditems/portal(locate(X,Y,Z),owner=ckey)
		new_item.invisibility = winget(src,"portal.invis","is-checked")=="true" ? 1 : 0
		new_item.Tele_X = x
		new_item.Tele_Y = y
		new_item.Tele_Z = z

		Query("INSERT INTO `[sql_objecttable]` (x,y,z,typepath,builtowner,uid,density,luminosity,invisibility,opacity,mesage,Tele_X,Tele_Y,Tele_Z,layer,infra_luminosity,icon_state,name) VALUES \
																					  ([new_item.x],[new_item.y],[new_item.z],'[new_item.type]','[new_item.builtowner]',[new_item.uid],[new_item.density],[new_item.luminosity],[new_item.invisibility],[new_item.opacity],'[new_item.mesage]',[new_item.Tele_X],[new_item.Tele_Y],[new_item.Tele_Z],[new_item.layer],[new_item.infra_luminosity],'[new_item.icon_state]','Portal');")

		winshow(src,"portal",0)

	Get_Loc()
		src << "X: [x]"
		src << "Y: [y]"
		src << "Z: [z]"